package com.hoshiicloud.commissioncalculation.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.commissioncalculation.feign.IdGeneratorService;
import com.hoshiicloud.commissioncalculation.mapper.CashRecordMapper;
import com.hoshiicloud.commissioncalculation.entity.CashRecord;
import com.hoshiicloud.commissioncalculation.param.CashApplyParam;
import com.hoshiicloud.commissioncalculation.service.CashRecordService;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.rpc.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 提现申请(CashRecord)表服务实现类
 *
 * @author makejava
 * @since 2019-09-09 10:18:01
 */
@Service("cashRecordService")
public class CashRecordServiceImpl extends ServiceImpl<CashRecordMapper, CashRecord> implements CashRecordService {

    @Autowired
    private IdGeneratorService idGeneratorService;

    /**
     * 提现申请
     *
     * @param param
     * @return
     */
    @Override
    public Response<CashRecord> save(CashApplyParam param) {

        try {
            String id = idGeneratorService.queryGeneratorId();

            if (id == null) {
                return Response.failed("申请失败");
            }

            CashRecord cashRecord = CashRecord.builder().id(Long.valueOf(id))
                    .companyid(param.getCompanyid())
                    .platformid(param.getPlatformid())
                    .storeid(param.getStoreid())
                    .memberId(param.getMemberId())
                    .amount(param.getAmount())
                    .cardNo(param.getCardNo())
                    .bankId(param.getBankId())
                    .createdby(param.getCreatedby())
                    .createdtime(new Date())
                    .status(1).build();
            int row = baseMapper.insert(cashRecord);

            if (row > 0) {
                return Response.success("申请成功", cashRecord);
            }

            return Response.failed("申请失败");
        } catch (Exception e) {
            log.error("提现申请异常：", e);
            return Response.failed("申请失败");
        }


    }
}